from django.utils import timezone
from django.db import models

# 登录账号密码
class Account(models.Model):
    user_id = models.CharField("学号", max_length=15, unique=True,primary_key=True)  # 登录账号：学号
    password = models.CharField(max_length=18, default="123456")  # 密码
    is_teacher = models.BooleanField(default=False)  # 判断身份：是否是教师
    is_super = models.BooleanField(default=False)  # 判断权限：是否是管理员
    locked = models.BooleanField(default=False)  # 判断账号异常：是否锁定
    avatar = models.FileField(upload_to='files/avatars/', null=True, blank=True) # 头像路径
    last_ip = models.CharField(max_length=255, default="127.0.0.1")  # 最后登录IP
    last_login_time = models.DateTimeField(default=timezone.now)  # 最后登录时间
    last_visitorId = models.CharField(max_length=255, default="0")  # 最后访问的课程ID

class User(models.Model):
    user_id = models.CharField("账号", max_length=15, unique=True,primary_key=True)
    name = models.CharField("姓名", max_length=20, default="user")
    gender = models.CharField("性别", max_length=2, default="")
    email = models.CharField("邮箱", max_length=254, default="")
    phone_number = models.CharField("手机号", max_length=11, default="")
    birthday = models.CharField("生日", null=True, max_length=100, default="2000-01-01")
    address = models.CharField("地址", max_length=255, default="")
    address_detail = models.CharField("详细地址", max_length=50, null=True)

    class Meta:
        abstract = True
        
# 教师表
class Teacher(User):
    school = models.CharField('学校', max_length=255, default='')
    college = models.CharField('学院', max_length=255, default='')
    class Meta:
        verbose_name = "教师信息"
        verbose_name_plural = verbose_name
    def __str__(self):
        return super().user_id

class Admin(User):

    user_id = models.CharField("账号", max_length=15, unique=True,primary_key=True)
    passWord = models.CharField('用户密码', db_column='pass_word', max_length=18, null=False, default='123456')
    # passWord = models.CharField(max_length=18, default="123456")
    class Meta:
        verbose_name = "管理员账号"
        verbose_name_plural = verbose_name
    def __str__(self):
        return super().user_id



# 学生表
class Student(User):


    
    school = models.CharField("学校", max_length=255, default="")
    college = models.CharField("学院", max_length=255, default="")
    major = models.CharField("专业", max_length=255, default="")
    cls = models.CharField("班级", max_length=255, default="")
    nation = models.CharField("民族", max_length=20, default="")
    # gender = models.CharField("性别", max_length=2, default="男")
    # birthday = models.CharField("生日", null=True, max_length=100, default="2000-01-01")
    country = models.CharField("国家", max_length=20, null=True, default="")
    # address = models.CharField("地址", max_length=50, default="")
    # address_detail = models.CharField("详细地址", max_length=50, null=True)
    # email = models.CharField("邮箱", max_length=254, default="")
    # phone_number = models.CharField("手机号", max_length=11, default="")
    id_card = models.CharField("身份证", max_length=18, null=True, unique=True)
    
    class Meta:
        verbose_name = "学生信息"
        verbose_name_plural = verbose_name
    def __str__(self):
        return super().user_id
# 学校表
class SchoolCollegeMajor(models.Model):
    id = models.AutoField(primary_key=True, verbose_name="序号")
    school = models.CharField(max_length=255, verbose_name="学校")
    college = models.CharField(max_length=255, verbose_name="学院")
    major = models.CharField(max_length=255, verbose_name="专业")
   



# 班课表
class Course(models.Model):
    STATUS_CHOICES = (
        ('pending', '待开始'),
        ('ongoing', '正在进行'),
        ('finished', '已结束'),
    )
    course_id = models.AutoField(primary_key=True, verbose_name="课程ID")
    name = models.CharField(max_length=100)  # 课程名称
    teachingCourseName = models.CharField(max_length=100, verbose_name="授课课程名称", default="")  # 授课课程名称
    description = models.TextField(default="")  # 课程描述
    term = models.CharField(max_length=50, verbose_name="学期", default="")
    creator = models.ForeignKey(Teacher, on_delete=models.CASCADE,verbose_name="创建者",default=None)  # 创建者
    created_at = models.DateTimeField(default=timezone.now)  # 创建时间
    updated_at = models.DateTimeField(default=timezone.now)  # 最后更新时间
    start_time = models.DateTimeField(default=timezone.now)  # 开始时间
    end_time = models.DateTimeField(default=timezone.now, null=True)  # 结束时间
    duration = models.DurationField(default=timezone.timedelta(hours=600))  # 课程时长
    allow_students_enrollment = models.BooleanField(default=True)  # 是否允许学生加入
    # 从别的班级导入并且共享文件。
    share_files = models.BooleanField(default=False)  # 是否共享班级文件
    
    imported_from_course_id = models.CharField(max_length=255, null=True, blank=True, default=None)  # 导入自其他课程
    course_cover = models.FileField(upload_to='files/covers/', null=True, blank=True) # 封面路径
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='ongoing', verbose_name="课程状态")
    homework_count = models.IntegerField(default=0, verbose_name="作业数量")
    exam_count = models.IntegerField(default=0, verbose_name="考试数量")
    experiment_count = models.IntegerField(default=0, verbose_name="实验数量")
    tasks = models.ManyToManyField('Task', through='Course2Task', verbose_name="任务")
    # 镜像名称
    image = models.CharField(max_length=255, null=True, blank=True, default='default')
    forum_messages = models.ManyToManyField('ForumMessage', through='Course2ForumMessage', verbose_name="论坛消息")
    homework_weight = models.IntegerField(default=30, verbose_name="作业权重")
    experiment_weight = models.IntegerField(default=30, verbose_name="实验权重")
    exam_weight = models.IntegerField(default=40, verbose_name="考试权重")

class ForumMessage(models.Model):
    id = models.AutoField(primary_key=True, verbose_name="论坛消息ID")
    title = models.CharField(max_length=100, verbose_name="标题")
    content = models.TextField(verbose_name="内容")
    created_at = models.DateTimeField(default=timezone.now, verbose_name="创建时间")
    updated_at = models.DateTimeField(default=timezone.now, verbose_name="更新时间")
    sender_type = models.CharField(max_length=20, verbose_name="发送者类型")
    student = models.ForeignKey(Student, on_delete=models.CASCADE, verbose_name="学生", null=True)
    teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE, verbose_name="老师", null=True)
    teacher_like_count = models.IntegerField(default=0, verbose_name="点赞数")
    student_like_count = models.IntegerField(default=0, verbose_name="点赞数")
    # 其他属性...

class Course2ForumMessage(models.Model):
    id = models.AutoField(primary_key=True, verbose_name="课程-论坛消息ID")
    course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name="课程ID", null=True)
    forum_message = models.ForeignKey(ForumMessage, on_delete=models.CASCADE, verbose_name="论坛消息ID", null=True)



class ForumMessageLike(models.Model):
    user_id = models.CharField("学号", max_length=20)
    message = models.ForeignKey(ForumMessage, on_delete=models.CASCADE, verbose_name="论坛消息")
    created_at = models.DateTimeField(default=timezone.now)


class Task(models.Model):
    # 任务表
    # 任务类型：实验、作业、考试
    TASK_TYPE_CHOICES = (
        ('实验', '实验'),
        ('作业', '作业'),
        ('考试', '考试'),
    )
    # 任务状态：未开始、进行中、已完成、已截止-未完成、已截止-已完成
    TASK_STATUS_CHOICES = (
        ('pending', '未开始'),
        ('ongoing', '进行中'),
        ('finished', '已完成'),
        # ('stopped-finished', '已截止-已完成'),
        # ('stopped-unfinished', '已截止-未完成'),
    )
    task_id = models.AutoField(primary_key=True, verbose_name="任务ID")
    task_type = models.CharField(max_length=20, verbose_name="任务类型")
    name = models.CharField(max_length=50, verbose_name="任务名称")
    start_time = models.DateTimeField(verbose_name="开始时间", default=timezone.now)
    end_time = models.DateTimeField(verbose_name="结束时间",default=timezone.now)
    imported_from_task_id = models.CharField(max_length=255, null=True, blank=True, default=None)  # 导入自其他任务
    status = models.CharField(max_length=20, choices=TASK_STATUS_CHOICES, default='pending', verbose_name="任务状态")
    # course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name="课程ID", null=True)
    teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE, verbose_name="教师ID", null=True)
    description = models.TextField(verbose_name="任务描述", default="", null=True, blank=True)
    score = models.FloatField(verbose_name="分数", default=0, null=True,blank=True)
    score_published = models.BooleanField(verbose_name="是否已经发布成绩", default=False)
    evaluation = models.TextField(verbose_name="评价", default="",null=True, blank=True)
    submissions = models.ManyToManyField('TaskSubmission', through='Task2Submission', verbose_name="提交")
    # 其他属性...

class Course2Task(models.Model):

    id = models.AutoField(primary_key=True, verbose_name="班级-任务ID")
    course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name="课程ID", null=True)
    task = models.ForeignKey(Task, on_delete=models.CASCADE, verbose_name="任务ID", null=True)
    

class TaskSubmission(models.Model):
    # 状态：待提交、已提交、待批改、已批改
    SUBMITTION_STATUS_CHOICES = (
        ('待批改', '待批改'),
        ('已批改', '已批改'),
    )
    id = models.AutoField(primary_key=True, verbose_name="提交ID")
    student = models.ForeignKey(Student, on_delete=models.CASCADE, verbose_name="学生")
    # task = models.ForeignKey(Task, on_delete=models.CASCADE, verbose_name="作业")
    submitted = models.BooleanField(default=False, verbose_name="是否完成")
    submitted_time = models.DateTimeField(default=timezone.now, verbose_name="提交时间", null=True)
    # score = models.FloatField(verbose_name="分数", default=0)
    submit_file = models.FileField("提交的文件", upload_to='data/task')
    marked_file = models.FileField("批改后的文件", upload_to='data/task', null=True, blank=True)
    # status = models.CharField(max_length=20,  default='待提交', verbose_name="任务状态")
    file_type = models.CharField(max_length=255, verbose_name="文件类型", default="")
    # 其他属性...

class Task2Submission(models.Model):
    id = models.AutoField(primary_key=True, verbose_name="任务-提交ID")
    task = models.ForeignKey(Task, on_delete=models.CASCADE, verbose_name="任务ID", null=True)
    submission = models.ForeignKey(TaskSubmission, on_delete=models.CASCADE, verbose_name="提交ID", null=True)

class TaskScore(models.Model):
    id = models.AutoField(primary_key=True, verbose_name="任务-分数ID")
    task = models.ForeignKey(Task, on_delete=models.CASCADE, verbose_name="任务ID", null=True)
    student = models.ForeignKey(Student, on_delete=models.CASCADE, verbose_name="学生ID", null=True)
    score = models.FloatField(verbose_name="分数", default=0)
    score_published = models.BooleanField(verbose_name="是否发布成绩", default=False)


class Course2UserEnrollment(models.Model):
    ROLE = (
        ('student', '学生'),
        ('teacher', '教师'),
        ('assistant', '助教'),
        ('creator', '创建者')
    )
    id = models.AutoField(primary_key=True, verbose_name="班级-学生ID")
    course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name="课程ID", null=True)
    user_id = models.CharField(max_length=20, verbose_name="学号")
    enroll_time = models.DateTimeField(default=timezone.now, verbose_name="加入时间")
    role = models.CharField(max_length=20, verbose_name="角色", default="student")
    unread_message_count = models.IntegerField(default=0, verbose_name="未读消息数")



class Container(models.Model):
    # 下面的学生，我需要改成Course2StuEnrollment里面的学生。如果学生退出班级，Course2StuEnrollment删除对应的学生，也要删除容器。
    # courseEnrollment = models.ForeignKey(Course2StuEnrollment, on_delete=models.CASCADE, verbose_name="班级-学生ID", null=True)
    student = models.ForeignKey(Student, on_delete=models.CASCADE, verbose_name="学生")
    task = models.ForeignKey(Task, on_delete=models.CASCADE, verbose_name="任务")
    # container_id = models.CharField("容器ID", max_length=255)  # 可能需要更改字段类型和长度以适应实际情况
    deployment_name = models.CharField("deployment名称", max_length=255, null=True, blank=True)  # 可能需要更改字段长度以适应实际情况
    created_time = models.DateTimeField("创建时间", default=timezone.now)
    last_accessed_time = models.DateTimeField("最后访问时间", default=timezone.now,null=True, blank=True)
    service_port = models.CharField("服务端口", max_length=255, null=True, blank=True)
    token = models.CharField("访问令牌", max_length=255, null=True, blank=True)
    pod_name = models.CharField("pod名称", max_length=255, null=True, blank=True)
    count_down_end_time = models.DateTimeField("倒计时结束时间", default=None,null=True, blank=True)
    
class TaskFiles(models.Model):
    task = models.ForeignKey(Task, on_delete=models.CASCADE)
    file = models.FileField(upload_to='files/tasks/', null=True, blank=True, verbose_name="任务文件")
    file_type = models.CharField(max_length=200, verbose_name="文件类型", default="")
    # 其他字段...

class DefaultParams(models.Model):
    # 系统默认参数
    param_name = models.CharField(max_length=255, verbose_name="参数名称")
    param_value = models.CharField(max_length=255, verbose_name="参数值", default=None, null=True, blank=True)
    param_type = models.CharField(max_length=255, verbose_name="参数类型", default='string')
    is_static = models.BooleanField( verbose_name="是否静态数据", default=False)
    
class LoginJournal(models.Model):
    user_id = models.CharField(max_length=20, verbose_name="学号")
    login_time = models.DateTimeField(default=timezone.now, verbose_name="登录时间")
    ip = models.CharField(max_length=255, verbose_name="IP地址", default=None, null=True, blank=True)
    visitorId = models.CharField(max_length=255, verbose_name="浏览器指纹", default=None, null=True, blank=True)

class LoginConfliction(models.Model):
    user_id_old = models.CharField(max_length=20, verbose_name="学号1")
    user_id_new = models.CharField(max_length=20, verbose_name="学号2")
    login_time = models.DateTimeField(default=timezone.now, verbose_name="登录时间")
    ip_old = models.CharField(max_length=255, verbose_name="IP地址1", default=None, null=True, blank=True)
    ip_new = models.CharField(max_length=255, verbose_name="IP地址2", default=None, null=True, blank=True)
    visitorId_old = models.CharField(max_length=255, verbose_name="浏览器指纹1", default=None, null=True, blank=True)
    visitorId_new = models.CharField(max_length=255, verbose_name="浏览器指纹2", default=None, null=True, blank=True)